; YALC client procedures
;
; Copyright (c) 2011, Solution
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;     * Redistributions of source code must retain the above copyright
;       notice, this list of conditions and the following disclaimer.
;     * Redistributions in binary form must reproduce the above copyright
;       notice, this list of conditions and the following disclaimer in the
;       documentation and/or other materials provided with the distribution.
;     * Neither the name of the Solution nor the
;       names of its contributors may be used to endorse or promote products
;       derived from this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
; DISCLAIMED. IN NO EVENT SHALL SOLUTION BE LIABLE FOR ANY
; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Declare client_SaveSettings(configpath.s)
Declare client_LoadSettings(configpath.s)
Declare client_GetLangList()
Declare client_LoadLang(lang.s)
Declare client_Connect(ip.s,port)
Declare client_IsIPAndPort(ip.s,port)

Procedure client_SaveSettings(configpath.s)
  If CreatePreferences(configpath)
    WritePreferenceString("Server",Server.s + ":" + Port.s)
    WritePreferenceString("Log","")
    WritePreferenceString("Debug","no")
    WritePreferenceString("Lang",Lang)
    ClosePreferences()
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
EndProcedure  

Procedure client_LoadSettings(configpath.s)
  If OpenPreferences(configpath)
    If Not ReadPreferenceString("Server","") = ""
      Server.s = StringField(ReadPreferenceString("Server",""),1,":")
      Port.s = StringField(ReadPreferenceString("Server",""),2,":")
    Else
      ProcedureReturn #False
    EndIf
    LogFile.s = ReadPreferenceString("Log","")
    If ReadPreferenceString("Debug","") = "yes"
      DebugEnabled = #True
    EndIf
    client_LoadLang(ReadPreferenceString("Lang",""))
    ClosePreferences()
    ProcedureReturn #True
  Else
    client_LoadLang("en")
    ProcedureReturn #False
  EndIf
EndProcedure

Procedure client_GetLangList()
  Global NewList LangList.s()
  CompilerIf #PB_Compiler_OS = #PB_OS_Linux Or #PB_Compiler_OS = #PB_OS_MacOS
    If ExamineDirectory(#langdir,RunDir.s + "lang/","*.lang")
      While NextDirectoryEntry(#langdir)
        If DirectoryEntryType(#langdir) = #PB_DirectoryEntry_File
          If OpenPreferences(RunDir.s + "lang\" + DirectoryEntryName(#langdir))
            If Not ReadPreferenceString("LangInfo","") = ""
              AddElement(LangList())
              LangList() = ReadPreferenceString("LangInfo","") + "[" + DirectoryEntryName(#langdir) + "]"
            EndIf
          EndIf
          ClosePreferences()
        EndIf
      Wend
      FinishDirectory(#langdir)
      If ListSize(LangList()) > 0
        ProcedureReturn #True
      Else
        ProcedureReturn #False
      EndIf
    EndIf
    ProcedureReturn #False
  CompilerElse
    If ExamineDirectory(#langdir,RunDir.s + "lang\","*.lang")
      While NextDirectoryEntry(#langdir)
        If DirectoryEntryType(#langdir) = #PB_DirectoryEntry_File
          If OpenPreferences(RunDir.s + "lang\" + DirectoryEntryName(#langdir))
            If Not ReadPreferenceString("LangInfo","") = ""
              AddElement(LangList())
              LangList() = ReadPreferenceString("LangInfo","") + " [" + ReplaceString(DirectoryEntryName(#langdir),".lang","") + "]"
            EndIf
            ClosePreferences()
          EndIf
        EndIf
      Wend
      FinishDirectory(#langdir)
      If ListSize(LangList()) > 0
        ProcedureReturn #True
      Else
        ProcedureReturn #False
      EndIf
    EndIf
    ProcedureReturn #False
  CompilerEndIf
EndProcedure

Procedure client_LoadLang(lang.s)
  If lang
    CompilerIf #PB_Compiler_OS = #PB_OS_Linux Or #PB_Compiler_OS = #PB_OS_MacOS
      OpenPreferences(RunDir.s + "lang/" + lang + ".lang")
    CompilerElse
      OpenPreferences(RunDir.s + "lang\" + lang + ".lang")
    CompilerEndIf
    Global lang_info$ = ReadPreferenceString("LangInfo","english [int]")
    Global lang_cantsave$ = ReadPreferenceString("CantSaveSettings","Can't save settings!")
    Global lang_conoptions$ = ReadPreferenceString("ConnectionOptions","Connection options")
    Global lang_inputlang$ = ReadPreferenceString("InputLang","Select your language")
    Global lang_inputserver$ = ReadPreferenceString("InputServer","Enter server IP")
    Global lang_inputport$ = ReadPreferenceString("InputPort","Enter server port")
    Global lang_okbutton$ = ReadPreferenceString("OkButton","Ok")
    Global lang_wrongipportformat$ = ReadPreferenceString("WrongIpPortFormat","Please enter ip and port")
    ClosePreferences()
    ProcedureReturn #True
  Else
    ; todo - language detection
    ProcedureReturn #False
  EndIf
EndProcedure

Procedure client_Connect(ip.s,port)
  
EndProcedure

Procedure client_IsIPAndPort(ip.s,port)
  If port > 0 And port <= 65535
    oc1 = Val(StringField(ip,1,"."))
    oc2 = Val(StringField(ip,2,"."))
    oc3 = Val(StringField(ip,3,"."))
    oc4 = Val(StringField(ip,4,"."))
    If oc1 > 0 And oc2 >= 0 And oc3 >=0 And oc4 > 0 And oc1 <= 254 And oc2 <= 254 And oc3 <= 254 And oc4 <= 254
      ProcedureReturn #True
    Else
      ProcedureReturn #False
    EndIf
  Else
    ProcedureReturn #False
  EndIf
EndProcedure
; IDE Options = PureBasic 4.60 (Windows - x86)
; CursorPosition = 39
; FirstLine = 18
; Folding = --
; EnableXP